package cn.com.hhrcw.service.impl;

import cn.com.hhrcw.entity.CvProjectInfo;
import cn.com.hhrcw.entity.CvTrain;
import cn.com.hhrcw.entity.CvUserEduInfo;
import cn.com.hhrcw.entity.CvUserWorkInfo;
import cn.com.hhrcw.service.ICvProjectInfoService;
import cn.com.hhrcw.service.ICvTimeDurationCompareService;
import cn.com.hhrcw.service.ICvTrainService;
import cn.com.hhrcw.service.ICvUserEduInfoService;
import cn.com.hhrcw.service.ICvUserWorkInfoService;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections.CollectionUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.enums.ExperienceTypeEnum;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yyg
 * @date 2022年10月27日
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ICvTimeDurationCompareServiceImpl implements ICvTimeDurationCompareService {
    @Autowired
    private ICvUserWorkInfoService cvUserWorknfoService;
    @Autowired
    private ICvUserEduInfoService cvUserEdunfoService;
    @Autowired
    private ICvTrainService cvTrainService;
    @Autowired
    private ICvProjectInfoService cvProjectnfoService;

    @Override
    public boolean isTimeDurationRepeat(int type, String userId, String resumeId, boolean isEdit,
                                        String id, Date startTime, Date endTime) {
        // 时间段被拆分两个了，校验下
        if(endTime.before(startTime)) return true;
        // 获取经历类型
        ExperienceTypeEnum typeEnum = ExperienceTypeEnum.getByValue(type);
        if(typeEnum == null) {
            log.info("isTimeDurationRepeat typeEnum null: {}",type);
            return true;
        }
        log.info("isTimeDurationRepeat typeEnum: {}",type);
        switch (typeEnum) {
            case WORK:
                // 工作/培训经历
                List<CvUserWorkInfo> workInfoList = cvUserWorknfoService.lambdaQuery()
                    .eq(CvUserWorkInfo::getResumeId, resumeId)
                    .eq(CvUserWorkInfo::getDelFlag, false).list();
                if(CollectionUtils.isEmpty(workInfoList)) return false;
                log.info("isTimeDurationRepeat workInfoList size: {}",workInfoList.size());
                List<CvUserWorkInfo> listCopy = isEdit ? workInfoList.stream().filter( x -> !x.getId().equals(id)).
                    collect(Collectors.toList()):workInfoList;
                // 遍历看下时间段是否有重合情况
                for(CvUserWorkInfo cvUserWorkInfo:listCopy) {
                    boolean isEqualStart = startTime.compareTo(cvUserWorkInfo.getWorkStartTime()) == 0;
                    if(isEqualStart) return true;
                    // 如果开始时间在某个时间段中级，有重合
                    if ( startTime.after(cvUserWorkInfo.getWorkStartTime()) &&
                        startTime.before(cvUserWorkInfo.getWorkStartTime())) {
                        return true;
                    }
                }
                break;
            case EDUCATION:
                // 教育经历
                List<CvUserEduInfo> eduInfoList = cvUserEdunfoService.lambdaQuery()
                    .eq(CvUserEduInfo::getResumeId, resumeId)
                    .eq(CvUserEduInfo::getDelFlag, false).list();
                if(CollectionUtils.isEmpty(eduInfoList)) return false;
                log.info("isTimeDurationRepeat eduInfoList size: {}",eduInfoList.size());
                List<CvUserEduInfo> listCopy2 = isEdit ? eduInfoList.stream().filter( x -> !x.getId().equals(id)).
                    collect(Collectors.toList()):eduInfoList;
                // 遍历看下时间段是否有重合情况
                for(CvUserEduInfo cvUserEduInfo:listCopy2) {
                    boolean isEqualStart = startTime.compareTo(cvUserEduInfo.getStartEduTime()) == 0;
                    if(isEqualStart) return true;
                    // 如果开始时间在某个时间段中级，有重合
                    if (startTime.after(cvUserEduInfo.getStartEduTime()) &&
                        startTime.before(cvUserEduInfo.getEndEduTime())) {
                        return true;
                    }
                }
                break;
            case TRAIN:
                // 培训经历
                List<CvTrain> trainList = cvTrainService.lambdaQuery()
                    .eq(CvTrain::getResumeId, resumeId)
                    .eq(CvTrain::getDelFlag, false).list();
                if(CollectionUtils.isEmpty(trainList)) return false;
                log.info("isTimeDurationRepeat trainList size: {}",trainList.size());
                List<CvTrain> listCopy3 = isEdit ? trainList.stream().filter( x -> !x.getId().equals(id)).
                    collect(Collectors.toList()):trainList;
                // 遍历看下时间段是否有重合情况
                for(CvTrain cvTrain:listCopy3) {
                    boolean isEqualStart = startTime.compareTo(cvTrain.getStartTime()) == 0;
                    if(isEqualStart) return true;
                    // 如果开始时间在某个时间段中级，有重合
                    if (startTime.after(cvTrain.getStartTime()) &&
                        startTime.before(cvTrain.getEndTime())) {
                        return true;
                    }
                }
                break;
            case PROJECT:
                // 项目经历
                List<CvProjectInfo> projectList = cvProjectnfoService.lambdaQuery()
                    .eq(CvProjectInfo::getResumeId, resumeId)
                    .eq(CvProjectInfo::getDelFlag, false).list();
                if(CollectionUtils.isEmpty(projectList)) return false;
                log.info("isTimeDurationRepeat projectList size: {}",projectList.size());
                List<CvProjectInfo> listCopy4 = isEdit ? projectList.stream().filter( x -> !x.getId().equals(id)).
                    collect(Collectors.toList()):projectList;
                // 遍历看下时间段是否有重合情况
                for(CvProjectInfo cvProjectInfo:listCopy4) {
                    boolean isEqualStart = startTime.compareTo(cvProjectInfo.getStartTime()) == 0;
                    if(isEqualStart) return true;
                    // 如果开始时间在某个时间段中级，有重合
                    if (startTime.after(cvProjectInfo.getStartTime()) &&
                        startTime.before(cvProjectInfo.getEndTime())) {
                        return true;
                    }
                }
                break;
            default:
                return true;
        }
        return false;
    }

    @Override
    public boolean isTimeDurationRepeat(int type, String userId, String resumeId, boolean isEdit,
                                        String id, Date startTime, String endWorkTime) {
        Date endTime;
        if(endWorkTime.equals(CommonConstant.TO_THIS_DAY)){
            endTime = new Date();
        }else {
            endTime = DateUtils.str2Date(endWorkTime, DateUtils.date_sdf.get());
        }
        return isTimeDurationRepeat(type, userId, resumeId, isEdit, id, startTime, endTime);
    }

}
